package com.example.controller;

import com.example.entity.SysCross;
import com.example.interceptor.TokenNotValidation;
import com.example.service.CrossService;
import com.example.utils.DealNumber;
import com.example.utils.ImgUtil;
import com.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/cross")
public class CrossController {

    @Autowired
    CrossService crossService;

    @TokenNotValidation
    @PostMapping("/creat")
    public void creat(@RequestBody SysCross cross, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.substring(7);
        Integer userId = JwtUtil.getUserId(token);
        cross.setCrossNumber(DealNumber.imgcode());
        cross.setCrossId(userId);
        cross.setUid(userId);
        crossService.add(cross);
    }

    @TokenNotValidation
    @PostMapping("/join")
    public void join(@RequestBody SysCross cross, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.substring(7);
        Integer userId = JwtUtil.getUserId(token);
        cross.setCrossId(userId);
        cross.setUid(userId);
        SysCross sysCross = crossService.selectByCrossNumber(cross.getCrossNumber());
        cross.setCrossName(sysCross.getCrossName());
        crossService.add(cross);
    }

    @TokenNotValidation
    @GetMapping("/getCross")
    public List<SysCross> getCross(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.substring(7);
        Integer userId = JwtUtil.getUserId(token);
        return crossService.getSysCross(userId);
    }
}
